/**
 * 对树进行从下往上，从左往右的遍历
 */

import { MNode } from "./mnodes";

export class MTreeWalker {
  private _root: MNode; // 根节点
  private _current: MNode | null;  // 当前遍历节点
  private _close: boolean;

  constructor (mnode: MNode) {
    this._root = mnode;
    this._current = mnode;
    this._close = false;
  }

  // 获取下一个节点
  next () {
    let mnode: MNode | null = this._current;
    let close: boolean = this._close;

    if (mnode == null) {
      return null;
    }

    if (!close && mnode.isContainer) {
      if (mnode.firstChild) {
        this._current = mnode.firstChild;
        this._close = false;
      } else {
        this._close = true;
      }
    } else if (mnode == this._root) {
      this._current = null;
    } else if (mnode.next == null) {
      this._current = mnode.parent;
      this._close = true;
    } else {
      this._current = mnode.next;
      this._close = false;
    }

    return { mnode, close };
  }
}

export default MTreeWalker;